package com.liziba.mq.direct.consumer.rabbit;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @auther LiZiBa
 * @date 2021/1/31 23:20
 * @description:
 **/
@Component
public class DirectConsumer {


    /**
     * 多个消费者监听同一个队列@RabbitListener注解置于方法上 如果置于类上导致报错
     * 直连交换机模式下是轮询消费
     * 此种消费的弊端在于如果部分消费者性能存在问题会影响消费速度
     */


    /**
     * 查看消费数据 -- 仍然是是平均消费
     * consumer1:{createTime=2021-01-31 23:29:33, messageId=09733a48-25b9-4b9a-944e-3638f5a40628, messageData=test message, hello!}
     * consumer1:{createTime=2021-01-31 23:29:37, messageId=0714a611-efd4-4510-8be8-412504a7c0ce, messageData=test message, hello!}
     * consumer2:{createTime=2021-01-31 23:29:36, messageId=13c5db7d-ffb9-4f21-a003-590fe91e44d0, messageData=test message, hello!}
     * consumer1:{createTime=2021-01-31 23:29:49, messageId=44f33c2e-22ff-4a83-9538-b971a920c9bc, messageData=test message, hello!}
     * consumer1:{createTime=2021-01-31 23:29:50, messageId=47709cc2-839c-4a59-b76b-d7a7159f03da, messageData=test message, hello!}
     * consumer1:{createTime=2021-01-31 23:29:50, messageId=d6f1ec00-5d5c-415b-aafe-4ea85017d5b8, messageData=test message, hello!}
     * consumer1:{createTime=2021-01-31 23:29:50, messageId=e07b5176-b11e-4d40-8793-72c2103fd089, messageData=test message, hello!}
     * consumer1:{createTime=2021-01-31 23:29:51, messageId=a99af852-5228-4460-ad3e-75677ca873af, messageData=test message, hello!}
     * consumer2:{createTime=2021-01-31 23:29:49, messageId=c3c03b56-de98-4751-b7c6-777f2fade808, messageData=test message, hello!}
     * consumer2:{createTime=2021-01-31 23:29:50, messageId=06e5cac8-15bb-45ec-8c0c-b9c92f6cf4b2, messageData=test message, hello!}
     * consumer2:{createTime=2021-01-31 23:29:50, messageId=8b85ecee-68bd-4b84-9236-99f18d265202, messageData=test message, hello!}
     * consumer2:{createTime=2021-01-31 23:29:50, messageId=15411942-a83c-4d53-8be6-da4ac9fde942, messageData=test message, hello!}
     * consumer2:{createTime=2021-01-31 23:29:50, messageId=4be74304-e7be-427f-b02a-3699590ac83a, messageData=test message, hello!}
     */



    // 监听的队列名称  DirectQueue
    @RabbitListener(queues = "DirectQueue")
    @RabbitHandler
    public void process(Map message){
        // {createTime=2021-01-31 23:12:00, messageId=d214b105-13e8-4811-b9b6-36430019fe44, messageData=test message, hello!}
        System.out.println("consumer1:" + message.toString());
    }

    // 监听的队列名称  DirectQueue
    @RabbitListener(queues = "DirectQueue")
    @RabbitHandler
    public void process2(Map message) throws InterruptedException {
        // 模拟消费能力较弱
        Thread.sleep(2000);
        System.out.println("consumer2:" + message.toString());
    }

}
